﻿using System;
using System.Collections.Generic;
using System.Text;
using BL;
using System.Data;
using Database;

namespace DB
{
    public class SubjectItemMapper:IMapper
    {
        List<Entity> subjectsList;
               
     
        #region IMapper Members

        public List<Entity> LoadAll()
        {
            DataTable dt = AccessDb.Instance.GetTable("SubjectItem");       
            subjectsList = new List<Entity>();
            foreach (DataRow dr in dt.Rows)
            {               
                SubjectItem subject = new SubjectItem();
                try
                {
                    subject.ID = Convert.ToInt64(dr["id"]);
                    subject.Faculty = (string)dr["facultyName"];
                    subject.Group = (string)dr["groupName"];
                    subject.Subject = (string)dr["subject"];
                    subject.LessonType = (LessonTypes)dr["typeName"];
                    subject.Hours = (int)dr["hours"];
                }
                catch
                {
                    continue;
                }
                subjectsList.Add(subject);
            }
            return subjectsList;
        }

        public Entity Find(long id)
        {
            DataTable dt = AccessDb.Instance.GetTable("SubjectItem");
            foreach (DataRow dr in dt.Rows)
            {
                if (Convert.ToInt64(dr["id"]) == id)
                {
                    string str1 = (string)dr["groupName"];
                    string str2 = (string)dr["subject"];
                    string str3 = (string)dr["facultyName"];
                    LessonTypes lt = (LessonTypes)int.Parse(dr["typeName"].ToString());
                    int hours = int.Parse(dr["hours"].ToString());
					SubjectItem subject = new SubjectItem(str3, str1, str2, 
						lt, hours);
					subject.ID = Convert.ToInt64(dr["id"]);
					return (Entity)subject;
                }
            }
            return null;
        }

        public void Insert(Entity e)
        {
            DataTable dt = AccessDb.Instance.GetTable("SubjectItem");
            SubjectItem subject = (SubjectItem)e;
            DataRow dr = dt.NewRow();
            dr["id"] = subject.ID;
            dr["facultyName"] = subject.Faculty;
            dr["groupName"] = subject.Group;
            dr["subject"] = subject.Subject;
            dr["typeName"] =  subject.LessonType;
            dr["hours"] = subject.Hours;
            dt.Rows.Add(dr);
        }

        public void DeleteEntity(Entity e)
        {
            DataTable dt = AccessDb.Instance.GetTable("SubjectItem");
            foreach (DataRow dr in dt.Rows)
            {
                if (Convert.ToInt64(dr["id"]) == e.ID)
                {
                    dr.Delete();
                    return;
                }
            }            
        }

        public void UpdateEntity(Entity e)
        {
            DataTable dt = AccessDb.Instance.GetTable("SubjectItem");
			SubjectItem subject = (SubjectItem)e;
            foreach (DataRow dr in dt.Rows)
            {
                if (Convert.ToInt64(dr["id"]) == e.ID)
                {
                    dr["facultyName"] = subject.Faculty;
                    dr["groupName"] = subject.Group;
                    dr["subject"] = subject.Subject;
                    dr["typeName"] = subject.LessonType;
                    dr["hours"] = subject.Hours;
                }
            }
        }

        public void ClearAll()
        {
            DataTable dt = AccessDb.Instance.GetTable("SubjectItem");
            foreach (DataRow dr in dt.Rows)
                dr.Delete();
        }

        #endregion
    }
}
